var gulp = require('gulp')
var autoprefixer = require('autoprefixer')
var cssnano = require('cssnano')
var pump = require('pump')
var $ = require('gulp-load-plugins')()
var browserSync = require("browser-sync").create()
var reload = browserSync.reload


gulp.task('stylus',function(){ //处理stylus(转化成css并且压缩)
    var plugins = [ 
        autoprefixer({browsers: ['last 1 version']}),
        cssnano()
    ]
    return gulp.src('./public/start/css/*.styl')
    .pipe($.stylus())
    .pipe($.postcss(plugins))
    .pipe($.cleanCss())
    .pipe(gulp.dest('./public/finally/css'))
})


gulp.task('js',function(cb){ //处理js(压缩js)
    pump([
        gulp.src('./public/start/js/*.js'),
        $.uglify(),
        gulp.dest('./public/finally/js')
    ],
    cb
 )
})

gulp.task('browser-sync', function() {
    browserSync.init({
        proxy: "http://localhost:8088/"
    });

    gulp.watch("./public/start/css/*.styl", ['stylus'])
    gulp.watch("./public/start/js/*.js", ['js'])
    gulp.watch("./views/*.ejs").on("change", browserSync.reload)
    gulp.watch("./public/finally/js/*.js").on("change", browserSync.reload)
    gulp.watch("./public/finally/css/*.css").on("change", browserSync.reload)
});

/*gulp.task('default',['stylus','js'],function(){ //监听文件的变化并且自动刷新
    browserSync.init({
        server: {
            proxy: "http://localhost:8088/"
        }
    })

    gulp.watch("./public/start/css/*.styl", ['stylus'])
    gulp.watch("./public/start/js/*.js", ['js'])
    gulp.watch("./views/*.ejs").on("change", browserSync.reload)
    gulp.watch("./public/finally/js/*.js").on("change", browserSync.reload)
    gulp.watch("./public/finally/css/*.css").on("change", browserSync.reload)
})*/

/*gulp.task('build',function(cb){ //压缩html和html页面的css和js
    var options = {
        removeComments: true,//清除HTML注释
        collapseWhitespace: true,//压缩HTML
        collapseBooleanAttributes: true,//省略布尔属性的值 <input checked="true"/> ==> <input />
        removeEmptyAttributes: true,//删除所有空格作属性值 <input id="" /> ==> <input />
        removeScriptTypeAttributes: true,//删除<script>的type="text/javascript"
        removeStyleLinkTypeAttributes: true,//删除<style>和<link>的type="text/css"
        minifyJS: true,//压缩页面JS
        minifyCSS: true//压缩页面CSS
    }
    return gulp.src('./other.html')
    .pipe($.htmlmin(options))
    .pipe($.rename('end.html'))
    .pipe(gulp.dest('./'))
})*/

